package com.robot.topology.robot.service.ant;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.robot.topology.graph.domain.Edge;
import com.robot.topology.robot.domain.Robot;
import com.robot.topology.robot.service.RobotManagerImpl;

public class RobotAntManagerImpl extends RobotManagerImpl {

	private Integer sizeRoutineTable;
	private Double sumWeights;
	private HashMap<Edge,Double> mapRT=null;

	public RobotAntManagerImpl(Robot robot) {
		super(robot);
		// TODO Auto-generated constructor stub
	}
	

	public void moveRobotOneStep(Integer step){
		Double aRandomNumber = Math.random();
		this.sumWeights =(double)0;

		//this.rTable = robot.getCurrentVertex().getRoutineTable().;
		this.mapRT=robot.getCurrentVertex().getRoutineTable().getTheRoutineTable();

		//Check the size of the routine table
		this.sizeRoutineTable=this.mapRT.size();

		//Get the sum of the weight values of the edge
		for (Double value : mapRT.values()) {
//			System.out.println("Values of the edges to choose from : "+value);
			sumWeights+=value;
		}
//		System.out.println("Sum of the weights for computing proba: "+sumWeights);
//				System.out.println("I choose a random number: "+aRandomNumber);

		chooseEdgeToFollow(step, aRandomNumber);
		System.out.println("I chose : "+this.edgeToFollow.toStringWeight());
		System.out.println("Its weight is : "+this.edgeToFollow.getWeight());
		
		//move to the proposed vertex
		//get the destination of the chosen edge
		this.vertexToMoveTo=this.edgeToFollow.getDestination();
		this.robot.setCurrentVertex(this.vertexToMoveTo);
//		System.out.println("I moved to "+this.robot.getCurrentVertex().toString());

		//Add the used edge to Path
		this.robot.getCurrentPath().addEdge(this.edgeToFollow);
		//Delete used edge from the unused edges Path
		this.robot.getUnusedEdges().deleteEdgeFromPath(edgeToFollow);
	}

	private void chooseEdgeToFollow(Integer step, Double aRandomNumber){
		int loopNumber=1;
		Double probaEdge=0.5;

		// Choose the edge to follow
		Iterator<Map.Entry<Edge,Double>> entries = this.mapRT.entrySet().iterator();

		while (entries.hasNext()) {

			Map.Entry<Edge,Double> entry = entries.next();
//			System.out.println("Computing proba :" +entry.getValue()+" / "+sumWeights);
			probaEdge=entry.getValue()/sumWeights;
//			System.out.println("Dealing with "+entry.getKey().toStringWeight());

//			System.out.println("aRandomNumber "+aRandomNumber);
//			System.out.println("probaEdge "+probaEdge);

			if (this.sizeRoutineTable==1 )
				this.edgeToFollow=entry.getKey();
			else if (this.sizeRoutineTable>1 ){
				if (loopNumber == 1){
					if (aRandomNumber<probaEdge){
						this.edgeToFollow=entry.getKey();
						System.out.println("Edge selected: "+entry.getKey().toStringWeight());
					}
					//else this.edgeToFollow=entries.next().getKey();
				} else if (loopNumber == 2){
					if (aRandomNumber>(1-probaEdge)){

						this.edgeToFollow=entry.getKey();
						System.out.println("Edge selected: "+entry.getKey().toStringWeight());
					}
				}
			}
			loopNumber++;
		}

	}

}
